import Vue from 'vue';

function toast(title, icon = 'none', duration = 1500) {
	uni.showToast({
		title: title,
		duration: duration,
		icon
	})
	return new Promise(resolve => {
		setTimeout(resolve, duration)
	});

}

function showModal(setting) {
	return new Promise((resolve, reject) => {
		uni.showModal({
			...setting,
			success(res) {
				if (res.confirm) {
					resolve()
				} else if (res.cancel) {
					reject()
				}
			},
			fail() {
				reject()
			}
		});
	});

}

// 跳转页面二次封装
function navto(url, showmodel = false, type) {
 if (showmodel ) {
 // if (showmodel && !store.state.user.islogin) {
  uni.$showModal({
    title: '提示',
    content: '您还未登录,是否去登录?'
   })
   .then(() => {
    store.commit('SET_NEXTURL', url)
    uni.navigateTo({
     url: '/pages/login/login'
    })
   })
 } else {
  if (type == 0 || !type) {
   uni.navigateTo({
    url: url
   })
  } else if (type == 1) {
   uni.redirectTo({
    url
   })
  } else {
   uni.reLaunch({
    url
   })
  }
 }
}
// 跳转协议
function toTreaty(data) {
	navto(`/module/treaty/treaty?title=${data}`)
}

export default {
	toast,
	showModal,
	navto,
	toTreaty
}
